#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "project.h"
#include "language.h"

int
page_header(char *title, char *info, char* request_url)
{
	fprintf(stdout, "Content-type:text/html\n\n");
	fprintf(stdout, "<html>\n");
	fprintf(stdout, "<head>\n");
	fprintf(stdout, "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
	fprintf(stdout, "<title>");
	if (title != NULL) {
		fprintf(stdout, "%s - ", title);
	}
	fprintf(stdout, "%s</title>\n", G_TITLE_INFO);
	//fprintf(stdout, "<link rel=stylesheet type=text/css href=styles.css>\n");
	page_style();
	fprintf(stdout, "</head>\n\n");
	fprintf(stdout, "<body>\n");

	fprintf(stdout, "<div id=outer>\n");// outer div begin

	
	fprintf(stdout, "<div id=header>\n");// header div begin


	fprintf(stdout, "<div class=top>\n");
	fprintf(stdout, "<table border=0 width=100%><tr>");
	fprintf(stdout, "<td align=left>");
	//printf(request_url);fflush(stdout);//debug
	char new_request_url[3][128];
	convert_language("zh-CN", request_url, new_request_url[0]);
	//printf(new_request_url[0]);fflush(stdout);//debug
	convert_language("en", request_url, new_request_url[1]);
	convert_language("ja", request_url, new_request_url[2]);
	//printf("[%s][%s][%s][%s]\n", page_name, new_request_url[0], new_request_url[1], new_request_url[2]);
	fprintf(stdout, "<a href='%s'>Chinese</a> ", new_request_url[0]);
	fprintf(stdout, "<a href='%s'>English</a> ", new_request_url[1]);
	fprintf(stdout, "<a href='%s'>Japanese</a>", new_request_url[2]);

	
	fprintf(stdout, "</td>");
	fprintf(stdout, "<td align=right><a href=login>%s</a></td>", G_LOGIN);
	fprintf(stdout, "</tr></table>\n");
	fprintf(stdout, "</div>\n");


	fprintf(stdout, "<div class=middle>\n");
	fprintf(stdout, "<form id=search method=GET action=%s>\n", SEARCH_PAGE_NAME);
	fprintf(stdout, "<table border=0><tr>");
	fprintf(stdout, "<td rowspan=2><a href=%s>", G_HOME_PAGE_NAME);
	fprintf(stdout, "<img src='http://%s/google_sm.gif' \\>", HOST);
	fprintf(stdout, "</a></td>");
	
	int ret;

	char hl_value[10];
	ret = getval(hl_value, "hl", request_url);
	//printf("[%d][%s][%s]", ret, hl, request_url);//debug
	if (ret == 0) {
		strcpy(hl_value, "en");
	}

	char cond_value[10], all_checked[10], name_checked[10], address_checked[10];
	ret = getval(cond_value, "cond", request_url);
	strcpy(all_checked, "");
	strcpy(name_checked, "");
	strcpy(address_checked, "");
	//printf("[%d][%s][%s]", ret, cond, request_url);//debug
	if (ret == 0) {
		strcpy(all_checked, "checked ");
	}else {
		if (strcmp(cond_value, "name") == 0) {
			strcpy(name_checked, "checked ");
		}else if (strcmp(cond_value, "addr") == 0) {
			strcpy(address_checked, "checked ");
		}else {
			strcpy(all_checked, "checked ");
		}
	}

	char key_value[256];
	ret = getval(key_value, "key", request_url);
	if (ret == 0) {
		strcpy(key_value, "");
	}
	fprintf(stdout, "<td><input type=hidden id=hl name=hl value=%s \\>", hl_value);
	fprintf(stdout, "<input type=text id=key name=key value='%s' size=50 \\></td>", key_value);
	fprintf(stdout, "<td><input type=submit value=%s \\></td>", G_SEARCH);
	fprintf(stdout, "</tr><tr>");
	fprintf(stdout, "<td>");
	fprintf(stdout, "<input type=radio id=cond name=cond value=all %s\\>%s&nbsp;&nbsp;", all_checked, G_ALL);
	fprintf(stdout, "<input type=radio id=cond name=cond value=name %s\\>%s&nbsp;&nbsp;", name_checked, G_NAME);
	fprintf(stdout, "<input type=radio id=cond name=cond value=addr %s\\>%s", address_checked, G_ADDRESS);
	fprintf(stdout, "</td>");
	fprintf(stdout, "<td>&nbsp;</td>");
	fprintf(stdout, "</tr></table>\n");
	fprintf(stdout, "</form>\n");
	fprintf(stdout, "</div>\n");


	fprintf(stdout, "<div class=bottom>\n");
	fprintf(stdout, "<table border=0 width=100%><tr>");
	fprintf(stdout, "<td align=left>%s</td>", title == NULL ? "&nbsp;" : title);
	fprintf(stdout, "<td align=right>%s</td>", info == NULL ? "&nbsp;" : info);
	fprintf(stdout, "</tr></table>\n");
	fprintf(stdout, "</div>\n");


	fprintf(stdout, "</div>\n");// header div end

	
	fprintf(stdout, "\t<div id=inner>\n");//inner div begin
	//fflush(stdout);
	return 0;
}

int
page_footer()
{
	fprintf(stdout, "\t</div>\n");// inner div end
	fprintf(stdout, "\t<div id=footer>\n");//footer div begin
	fprintf(stdout, "\t\t<br \\n>%s\n", G_COPYRIGHT_INFO);
	fprintf(stdout, "\t</div>\n");//footer div end
	fprintf(stdout, "</div>\n");// outer div end
	fprintf(stdout, "</body>\n");
	fprintf(stdout, "</html>\n\n");
	//fflush(stdout);
	return 0;
}

int
page_style()
{
	fprintf(stdout, "<style>\n");
	fprintf(stdout, "body\n{\n");
	fprintf(stdout, "margin:0 10px 0 10px;\n");
	fprintf(stdout, "padding:0;\n");
	fprintf(stdout, "}\n");

	fprintf(stdout, "img\n{\n");
	fprintf(stdout, "\tborder:0px;\n");
	fprintf(stdout, "\tdisplay:block;\n");
	fprintf(stdout, "}\n");

	fprintf(stdout, "#outer\n{\n");
 	fprintf(stdout, "height:100%;");
	//fprintf(stdout, "\tborder-bottom:5px solid #c9d7f1;\n");
 	fprintf(stdout, "}\n");

	fprintf(stdout, "#header \n{\n");
 	fprintf(stdout, "}\n");

	fprintf(stdout, "#header .top\n{\n");
	fprintf(stdout, "\tborder-bottom:1px solid #c9d7f1;\n");
 	fprintf(stdout, "}\n");

	fprintf(stdout, "#header .middle\n{\n");
	fprintf(stdout, "\tmargin-top:10px;\n");
 	fprintf(stdout, "}\n");

	fprintf(stdout, "#header .bottom\n{\n");
	fprintf(stdout, "\tbackground:#d5ddf3;\n");
	fprintf(stdout, "\tmargin-top:-10px;\n");
	fprintf(stdout, "\tborder-top:1px solid #3366cc;\n");
 	fprintf(stdout, "}\n");

	fprintf(stdout, "#inner\n{\n");
 	fprintf(stdout, "\tmargin:10px 10px 10px 10px;\n");
 	fprintf(stdout, "}\n");

	fprintf(stdout, "#footer\n{\n");
 	fprintf(stdout, "\ttext-align:center;\n");
	fprintf(stdout, "\tborder-top:1px solid #c9d7f1;\n");
 	fprintf(stdout, "}\n");

	fprintf(stdout, "</style>\n");

	return 0;
}

int
page_error(int ret)
{
	fprintf(stdout, "<a href=%s>%s</a>, %s<br \\>\n",
		G_HOME_PAGE_NAME, G_SORRY, G_ERR_INFO);
	return 0;
}
int
getval(char *v, char *n, char *s)
{
	char *p;
	int i = 0, flag = 0;
	if ((p = strstr(s, n)) != 0) {
		flag = 1;
		while (*p != '=') {
			p++;
		}
		++p;
		while (*p != '&' && *p != '\0') {
			v[i] = *p;
			p++;
			i++;
		}
		v[i] = '\0';
	}
	return flag;
}

void init_pedigree_list_unit(struct pedigree_list_unit *p_l_unit)
{
	p_l_unit->self.id = -1;
	p_l_unit->self.name = NULL;
	p_l_unit->wife = NULL;
	p_l_unit->father.id = -1;
	p_l_unit->father.name = NULL;
	p_l_unit->mother.id = -1;
	p_l_unit->mother.name = NULL;
	p_l_unit->children = NULL;
	p_l_unit->dis = NULL;
	p_l_unit->wife_num = 0;
	p_l_unit->children_num = 0;
}

